//
//  GMScrollBar.m
//  GomeEShop
//
//  Created by tangli on 15/3/23.
//  Copyright (c) 2015年 Gome. All rights reserved.
//

#import "GMCategaryScrollBar.h"
#import "GMCategaryBar.h"

@interface GMCategaryScrollBar()
<
UIScrollViewDelegate,
GMCategaryBarDelegate
>

@property (nonatomic, weak) GMCategaryBar *categaryBar; // 分类选项卡

@property (nonatomic, weak) UIScrollView *scrv;

@property (nonatomic, assign) CGFloat fltControllerW; // 每个控制器的宽度

@property (nonatomic, assign) BOOL isShowCategaryBar; // 是否显示categaryBar的一个开关

@property (nonatomic, assign) BOOL isCanScroll; // 是否支持scrollView的滚动

@property (nonatomic, strong) NSMutableArray *maryTitle; // 分类选项卡中的文字数组

@property (nonatomic, assign) NSInteger intIndex;  // 纪录上一个分类按钮的索引

@end

@implementation GMCategaryScrollBar

- (instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame])
    {
        _isShowCategaryBar = YES;
        
        // 创建scrollView
        UIScrollView *scrv = [[UIScrollView alloc]init];
        CGFloat fltScrollViewY = (_isShowCategaryBar) ? (CATEGARYBAR_H) : (0);
        scrv.frame = CGRectMake(0,
                                fltScrollViewY,
                                self.frame.size.width,
                                self.frame.size.height - fltScrollViewY);

        _fltControllerW = scrv.frame.size.width;
        _fltControllerH = scrv.frame.size.height;
        
        scrv.delegate = self; // 设置scrollView的代理
        
        // 隐藏水平方向的滚动条
        scrv.showsHorizontalScrollIndicator = NO;
        // 隐藏竖直方向的滚动条
        scrv.showsVerticalScrollIndicator = NO;
        
        // 取消弹簧效果
        scrv.bounces =NO;
        
        //增加上中左右额外的区域
        scrv.contentInset =UIEdgeInsetsMake(0, 0, 0, 0);
        
        scrv.pagingEnabled = YES;
        
        _scrv = scrv;
        
        [self addSubview:scrv];
    }
    return self;
}

// 类创建方法
+ (instancetype)categaryScrollBar
{
    return [[self alloc]initWithFrame:CGRectMake(0,
                                                GMK_NavBar_H,
                                                screenW,
                                                screenH - GMK_NavBar_H)];
}

// 控制器数组的set方法
- (void)setMaryVC:(NSMutableArray *)maryVC
{
    _maryVC = maryVC;
    
    if (maryVC.count == 0 || maryVC.count == 1)
    {
        _isShowCategaryBar = NO;
        CGFloat fltScrollViewY = _isShowCategaryBar?CATEGARYBAR_H:0;
        _fltControllerH = self.frame.size.height - fltScrollViewY;
        _scrv.frame = CGRectMake(0, fltScrollViewY, screenW, _fltControllerH);
    }
    else
    {
        _isShowCategaryBar = YES;
    }
    
    if (maryVC.count > 0)
    {
        UIViewController<GMCategaryScrollBarDelegate> *vc = _maryVC[0];
        vc.view.frame = CGRectMake(0, 0, _fltControllerW, _scrv.frame.size.height);
        
        [_scrv addSubview:vc.view];
        
        // 设置第一个控制器为代理
        _delegate = vc;
        
        // 设置categaryBar的内容尺寸
        _scrv.contentSize =  CGSizeMake(_fltControllerW * _maryVC.count, 0);

    }
}

- (void)categaryBarTitle:(NSArray *)aryTitle withIsEqualBtn:(BOOL)isEqualBtn
{
    _maryTitle = [NSMutableArray arrayWithArray:aryTitle];
    
    if (aryTitle.count == 0 || aryTitle.count == 1)
    {
        _isShowCategaryBar = NO;
        
        CGFloat fltScrollViewY = _isShowCategaryBar?CATEGARYBAR_H:0;
         _fltControllerH = self.frame.size.height - fltScrollViewY;
        _scrv.frame = CGRectMake(0, fltScrollViewY, screenW, self.frame.size.height - fltScrollViewY);
    }
    else
    {
        _isShowCategaryBar = YES;
    }
    
    [self categaryBar:isEqualBtn];
}

// UIView数组的set方法
- (void)setMaryView:(NSMutableArray *)maryView
{
    _maryView = maryView;
    
    if (maryView.count == 0 || maryView.count == 1)
    {
        _isShowCategaryBar = NO;
        CGFloat fltScrollViewY = (_isShowCategaryBar) ? (CATEGARYBAR_H) : (0);
        _fltControllerH = self.frame.size.height - fltScrollViewY;
        _scrv.frame = CGRectMake(0, fltScrollViewY, screenW, screenH - fltScrollViewY);
    }
    else
    {
        _isShowCategaryBar = YES;
    }
    
    if (_maryView.count > 0)
    {
        UIView *view = _maryView[0];
        view.frame = CGRectMake(0, 0, _fltControllerW, _scrv.frame.size.height);
        
        [_scrv addSubview:view];
        
        // 设置categaryBar的内容尺寸
        _scrv.contentSize =  CGSizeMake(_fltControllerW * _maryView.count, 0);
    }
}

// 是否显示categaryBar的一个开关
- (void)categaryBarShow:(BOOL)isShowCategaryBar
{
    _isShowCategaryBar = isShowCategaryBar;
    
    if (_isShowCategaryBar == NO)
    {
        CGFloat fltScrollViewY = (_isShowCategaryBar) ? (CATEGARYBAR_H) : (0);
        _fltControllerH = self.frame.size.height - fltScrollViewY;
        _scrv.frame = CGRectMake(0, fltScrollViewY, screenW, self.frame.size.height - fltScrollViewY);
    }
}

// 是否支持scrollView的滚动
- (void)scrollBarScroll:(BOOL)isCanScroll
{
    _isCanScroll = isCanScroll;
    
    if (_isCanScroll == NO)
    {
        _scrv.scrollEnabled = NO;
    }
}

// 创建分类bar
- (void)categaryBar:(BOOL)isEqualBtn
{
    if (_categaryBar == nil)
    {
        GMCategaryBar *categaryBar = [GMCategaryBar categaryBarWithArrayTitle:self.maryTitle
                                                           dividerIsEqualBtnW:isEqualBtn];
        categaryBar.frame = CGRectMake(0, 0, screenW, CATEGARYBAR_H);
        categaryBar.delegateCategary = self;
        _categaryBar = categaryBar;
        [self addSubview:categaryBar];
    }
}


#pragma mark - 代理GMCategaryBarDelegate

- (void)categaryBarOptionalDelegate:(GMCategaryBar *)categaryBar withBtnIndex:(NSInteger)intIndex
{
    CGFloat offsetX = intIndex * _fltControllerW;
    
    // 自动滑动categaryBar的相应的距离
    [_scrv setContentOffset:CGPointMake(offsetX, 0) animated:YES];
}


#pragma mark - 代理UIScrollViewDelegate

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if ([scrollView isEqual:_scrv])
    {
        NSInteger intIndex = floor(scrollView.contentOffset.x / scrollView.frame.size.width + 0.5);
        
        [_categaryBar automaticScrollDivider:intIndex];
    }

}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_intIndex == floor(scrollView.contentOffset.x / scrollView.frame.size.width))
    {
        return;
    }
    
    if ([scrollView isEqual:_scrv])
    {
        _intIndex = floor(scrollView.contentOffset.x / scrollView.frame.size.width);
        
        if (_maryVC.count != 0)
        {
            // 如果代理存在，则清除
            if (_delegate)
            {
                _delegate = nil;
            }
            
            // 根据索引创建对应的控制器
            UIViewController<GMCategaryScrollBarDelegate> *vc = _maryVC[_intIndex];
            
            _delegate = vc;
            
            if ([_scrv.subviews containsObject:vc.view]) // 第一次进入控制器时不调用代理方法
            {
                // 当滚动到控制器或当前View时的代理
                if ([_delegate respondsToSelector:@selector(categaryScrollBarDelegateScrollVC:withIndex:)])
                {
                    [_delegate categaryScrollBarDelegateScrollVC:self withIndex:_intIndex];
                }
            }
            else if(_isReponseDelegate) /// 如果第一次需要实现代理
            {
                // 当滚动到控制器或当前View时的代理
                if ([_delegate respondsToSelector:@selector(categaryScrollBarDelegateScrollVC:withIndex:)])
                {
                    [_delegate categaryScrollBarDelegateScrollVC:self withIndex:_intIndex];
                }
            }
            
            // 根据索引创建对应的控制器并添加到scrollView中
            [self createVC:_intIndex];
        }
        
        if (_maryView != 0)
        {
            // 当滚动到控制器或当前View时的代理
            if ([_delegate respondsToSelector:@selector(categaryScrollBarDelegateScrollVC:withIndex:)])
            {
                [_delegate categaryScrollBarDelegateScrollVC:self withIndex:_intIndex];
            }
            
            // 根据索引创建对应的UIView并添加到scrollView中
            [self createView:_intIndex];
        }
        
    }
}

// 根据索引创建对应的控制器并添加到scrollView中
- (void)createVC:(NSInteger)intIndex
{
    // 根据索引创建对应的控制器
    UIViewController *vc = _maryVC[intIndex];
    
    if (![_scrv.subviews containsObject:vc.view])
    {
        vc.view.frame = CGRectMake(_fltControllerW * intIndex, 0, _fltControllerW, _fltControllerH);
        
        [_scrv addSubview:vc.view];
    }
}

// 根据索引创建对应的UIView并添加到scrollView中
- (void)createView:(NSInteger)intIndex
{
    // 根据索引创建对应的控制器
    UIView *view = _maryView[intIndex];
    
    if (![_scrv.subviews containsObject:view])
    {
        view.frame = CGRectMake(_fltControllerW * intIndex, 0, _fltControllerW, _fltControllerH);
        
        [_scrv addSubview:view];
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView
{
    if (_intIndex == floor(scrollView.contentOffset.x / scrollView.frame.size.width))
    {
        return;
    }
    
    _intIndex = (NSInteger)(scrollView.contentOffset.x / scrollView.frame.size.width);
    
    if (_maryVC.count != 0)
    {
        // 如果代理存在，则清除
        if (_delegate)
        {
            _delegate = nil;
        }
        
        // 根据索引创建对应的控制器
        UIViewController<GMCategaryScrollBarDelegate> *vc = _maryVC[_intIndex];
        
        _delegate = vc;
        
        if ([_scrv.subviews containsObject:vc.view]) // 第一次进入控制器时不调用代理方法
        {
            // 当滚动到控制器或当前View时的代理
            if ([_delegate respondsToSelector:@selector(categaryScrollBarDelegateScrollVC:withIndex:)])
            {
                [_delegate categaryScrollBarDelegateScrollVC:self withIndex:_intIndex];
            }
        }
        else if(_isReponseDelegate) /// 如果第一次需要实现代理
        {
            // 当滚动到控制器或当前View时的代理
            if ([_delegate respondsToSelector:@selector(categaryScrollBarDelegateScrollVC:withIndex:)])
            {
                [_delegate categaryScrollBarDelegateScrollVC:self withIndex:_intIndex];
            }
        }
        
        // 根据索引创建对应的控制器并添加到scrollView中
        [self createVC:_intIndex];
    }
    
    if (_maryView != 0)
    {
        // 当滚动到控制器或当前View时的代理
        if ([_delegate respondsToSelector:@selector(categaryScrollBarDelegateScrollVC:withIndex:)])
        {
            [_delegate categaryScrollBarDelegateScrollVC:self withIndex:_intIndex];
        }
        
        // 根据索引创建对应的UIView并添加到scrollView中
        [self createView:_intIndex];
    }
}

// 提供一个接口，使上面的分类框和下面的View自动滚动到相应的索引位置
- (void)automaticScroll:(NSInteger)intIndex
{
    CGFloat offsetX = intIndex * screenW;
    
    // 自动滑动categaryBar的相应的距离
    [_scrv setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
    [_categaryBar automaticScrollDivider:intIndex];
}


@end
